//
//  BarCodeController.m
//  BarCode
//
//  Created by Hunk on 12-2-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BarCodeController.h"

@implementation BarCodeController
@synthesize delegate   = _delegate;
@synthesize dataSource = _dataSource;

- (id)init
{
    if((self = [super init]))
    {
        // Init ZBarReaderViewController
        zbarReaderViewController = [[ZBarReaderViewController alloc] init];
        [zbarReaderViewController setReaderDelegate:self];
        
        // ZBarImageScanner
        ZBarImageScanner *zbarImageScanner = zbarReaderViewController.scanner;
        [zbarImageScanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
        
        zbarReaderViewController.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        zbarReaderViewController.cameraMode = ZBarReaderControllerCameraModeSampling;
        
        // 隐藏ZBar控制按钮
        zbarReaderViewController.showsZBarControls = NO;
        
        // 是否显示扫描框
        zbarReaderViewController.tracksSymbols = NO;
        
        // 扫描失败时是否显示帮助
        zbarReaderViewController.showsHelpOnFail = NO;
    }
    return self;
}

- (void)setDelegate:(id<BarCodeControllerDelegate>)delegate
{
    if(_delegate != delegate)
    {
        [_delegate release];
        
        _delegate = [delegate retain];
    }
}

- (void)setDataSource:(id<BarCodeControllerDataSource>)dataSource
{
    if(_dataSource != dataSource)
    {
        [_dataSource release];
        
        _dataSource = [dataSource retain];
    }    
}

#pragma mark -
#pragma mark scanBarCode
- (void)scanBarCodeOnViewController:(UIViewController *)viewController
{
    if(_viewController != viewController)
    {
        [_viewController release];
        
        _viewController = [viewController retain];
    }
    
    if([_dataSource respondsToSelector:@selector(barCodeOverlayView:)])
    {   
        zbarReaderViewController.cameraOverlayView = [_dataSource barCodeOverlayView:self];
    }
    
    [viewController presentModalViewController:zbarReaderViewController animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
    
    ZBarSymbol *zbarSymbol = nil;
    for(zbarSymbol in results)
    {
        break;
    }
    
    NSString *result = zbarSymbol.data;
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    if([_delegate respondsToSelector:@selector(didFinishScan:result:image:)])
    {
        [zbarReaderViewController dismissModalViewControllerAnimated:YES];
        
        [_delegate didFinishScan:self result:result image:image];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self cancelScanBarCode];
}

- (void) readerControllerDidFailToRead:(ZBarReaderController*) reader withRetry:(BOOL) retry;
{
    [self cancelScanBarCode];
}

- (void)cancelScanBarCode
{
    if([_delegate respondsToSelector:@selector(didFinishScan:result:image:)])
    {
        [zbarReaderViewController dismissModalViewControllerAnimated:YES];
        
        [_delegate didFinishScan:self result:nil image:nil];
    }
}

- (void)dealloc
{
    [zbarReaderViewController release];
    [_delegate release];
    [_viewController release];
    
    [super dealloc];
}
@end
